<?php
/**
 * Created by PhpStorm.
 * User: wangye
 * Date: 19-2-13
 * Time: 下午4:19
 */
use Lib\GetRedis;
require './vendor/autoload.php';

$redis = GetRedis::getInstance();

$key = 'mail:task';

while (true){
    $timeout = 2;
    $message = $redis->brPop($key,$timeout);#brPop阻塞读取
    var_dump($message);
    if (empty($message)){
        continue;
    }
    $message = json_decode($message[1],true);

    $begin  = microtime(true);
    seed_mail($message);
    $end  = microtime(true);
    $elapse = (int)(($end-$begin)*1000);

    if ($elapse > 100){
        continue;
    }else{
        $sleep_time = (100- $elapse)*1000;
        echo 'sleep',$sleep_time,'us',PHP_EOL;
        usleep($sleep_time);
    }

    echo 'send mail',PHP_EOL;
}

function seed_mail($message){
    echo '发送邮件ing...',PHP_EOL;
    usleep(10*1000);
    echo '发送完毕',PHP_EOL;
}